/*----------------------------------------------------------------------------*\
| /gamemode/cl_wswitch.lua             | weapon-switching system         | CLI |
\*----------------------------------------------------------------------------*/

wswitch = {}
wswitch.Switch = false
wswitch.Category = 0
wswitch.Slot = 0

wswitch.Names     = {}
wswitch.Names[-1] = "[NO WEAPON]"
wswitch.Names[ 0] = "FISTS"
wswitch.Names[ 1] = "KNIFE"
wswitch.Names[ 2] = "FLASHLIGHT"
wswitch.Names[ 3] = "CHAINSAW"
wswitch.Names[ 4] = "PISTOL"
wswitch.Names[ 5] = "REVOLVER"
wswitch.Names[ 6] = "[RESERVED]"
wswitch.Names[ 7] = "[RESERVED]"
wswitch.Names[ 8] = "ASSAULT RIFLE"
wswitch.Names[ 9] = "SUBMACHINE GUN"
wswitch.Names[10] = "[RESERVED]"
wswitch.Names[11] = "[RESERVED]"
wswitch.Names[12] = "COMBAT SHOTGUN"
wswitch.Names[13] = "HUNTING SHOTGUN"
wswitch.Names[14] = "RIFLE"

wswitch.Class     = {}
wswitch.Class[-1] = "..."
wswitch.Class[ 0] = "weapon_fists"
wswitch.Class[ 1] = "weapon_huntingknife"
wswitch.Class[ 2] = "weapon_flashlight"
wswitch.Class[ 3] = "weapon_chainsaw"
wswitch.Class[ 4] = "weapon_9mmpistol"
wswitch.Class[ 5] = "weapon_revolver"
wswitch.Class[ 6] = "..."
wswitch.Class[ 7] = "..."
wswitch.Class[ 8] = "weapon_assaultrifle"
wswitch.Class[ 9] = "weapon_submachinegun"
wswitch.Class[10] = "..."
wswitch.Class[11] = "..."
wswitch.Class[12] = "weapon_combatshotgun"
wswitch.Class[13] = "weapon_huntingshotgun"
wswitch.Class[14] = "weapon_rifle"

wswitch.MyWeapons       = {}
wswitch.MyWeapons[ 0] = false
wswitch.MyWeapons[ 1] = false
wswitch.MyWeapons[ 2] = false
wswitch.MyWeapons[ 3] = false
wswitch.MyWeapons[ 4] = false
wswitch.MyWeapons[ 5] = false
wswitch.MyWeapons[ 6] = false
wswitch.MyWeapons[ 7] = false
wswitch.MyWeapons[ 8] = false
wswitch.MyWeapons[ 9] = false
wswitch.MyWeapons[10] = false
wswitch.MyWeapons[11] = false
wswitch.MyWeapons[12] = false
wswitch.MyWeapons[13] = false
wswitch.MyWeapons[14] = false

wswitch.Count     = {}
wswitch.Count[ 0] = 0
wswitch.Count[ 1] = 4
wswitch.Count[ 2] = 2
wswitch.Count[ 3] = 2
wswitch.Count[ 4] = 2
wswitch.Count[ 5] = 1
wswitch.Count[ 6] = 0
wswitch.Count[ 7] = 0
wswitch.Count[ 8] = 0
wswitch.Count[ 9] = 0
wswitch.Count[10] = 0

wswitch.CurWeapon = 0
wswitch.SelectWeapon = 0

wswitch.Toggle = false


--[[ 
	Usermessage hook for weapons switching.
--]]
function SetWeapons( um )
	local windex = um:ReadShort()
	local state = um:ReadBool()
	wswitch.MyWeapons[windex] = state -- I'm too lazy to do a check
end
usermessage.Hook( "SetWeapon", SetWeapons ) 


function wswitch.CheckBinds(pl,bind)
	if (string.Left(bind,4)) == "slot" then
		slotnum=tonumber(string.sub(bind,5))
		if (pl:GetMoveType()!=MOVETYPE_LADDER) then
			if (!wswitch.Toggle) then
				if (wswitch.Category==slotnum) then
					for i=1,wswitch.Count[wswitch.Category] do
						j=wswitch.Slot+i
						if (j>=wswitch.Count[wswitch.Category]) then
							j=j-wswitch.Count[wswitch.Category]
						end
						if (wswitch.MyWeapons[ToWeapon(wswitch.Category,j)]) then
							wswitch.Slot=j
							break
						end
					end
				else
					for i=0,wswitch.Count[slotnum]-1 do
						if (wswitch.MyWeapons[ToWeapon(slotnum,i)]) then
							wswitch.Category=slotnum
							wswitch.Slot=i
							break
						end
					end
				end
				wswitch.SelectWeapon=ToWeapon(wswitch.Category,wswitch.Slot)
				wswitch.Toggle=true
			else
				wswitch.Toggle=false
			end
			wswitch.Switch=true
			return false
		else
			return bind == "slot"..slotnum
		end
	end
	if bind == "invnext" then
		if (pl:GetMoveType()!=MOVETYPE_LADDER) then
			if (!wswitch.Toggle) then
				NW=NextWeapon()
				if (NW) then wswitch.SelectWeapon=NW end
				wswitch.Switch=true
				wswitch.Toggle=true
			else
				wswitch.Toggle=false
			end
			return false
		else
			return bind == "invnext"
		end
	end
	
	if bind == "invprev" then
		if (pl:GetMoveType()!=MOVETYPE_LADDER) then
			if (!wswitch.Toggle) then
				PW=PrevWeapon()
				if (PW) then wswitch.SelectWeapon=PW end
				wswitch.Switch=true
				wswitch.Toggle=true
			else
				wswitch.Toggle=false
			end
			return false
		else
			return bind == "invprev"
		end
	end
	
	if bind == "+attack" then
		if (wswitch.Switch==true) then
			wswitch.CurWeapon=wswitch.SelectWeapon
			LocalPlayer():ConCommand("selectweapon "..wswitch.Class[wswitch.SelectWeapon].."\n") /*ToWeapon(wswitch.Category,wswitch.Slot)*/
			LocalPlayer():ConCommand("-attack\n")
			wswitch.Switch=false
			wswitch.Toggle=false
			return true
		end
	end
	
	if bind == "cancelselect" then
		if (wswitch.Switch==true) then
			wswitch.Switch=false
			return false
		end
	end
end
hook.Add( "PlayerBindPress", "CheckBinds", wswitch.CheckBinds )

function ToWeapon(cat,slot)
	if (cat==1) then
		if (slot==0) then return  0 end
		if (slot==1) then return  1 end
		if (slot==2) then return  2 end
		if (slot==3) then return  3 end
	elseif (cat==2) then
		if (slot==0) then return  4 end
		if (slot==1) then return  5 end
		if (slot==2) then return  6 end
		if (slot==3) then return  7 end
	elseif (cat==3) then
		if (slot==0) then return  8 end
		if (slot==1) then return  9 end
		if (slot==2) then return 10 end
		if (slot==3) then return 11 end
	elseif (cat==4) then
		if (slot==0) then return 12 end
		if (slot==1) then return 13 end
	elseif (cat==5) then
		if (slot==0) then return 14 end
	end
	return -1
end

/******************************************************************************\
 *                            LEGACY FUNCTION                                 *
 *          TODO: Search for any references. If none can be found,            *
 *                         remove this function.                              *
\******************************************************************************/
function FromWeapon(w)
	if (w==0) then return {2,0} end
	if (w==1) then return {2,1} end
	if (w==2) then return {3,0} end
	if (w==3) then return {3,1} end
	if (w==4) then return {4,0} end
	if (w==5) then return {4,1} end
end

function NextWeapon()
	for i=1,#wswitch.Names do
		j=wswitch.SelectWeapon+i
		if (j>#wswitch.Names) then j=j-#wswitch.Names end
		if wswitch.MyWeapons[j] then return j end
	end
	return wswitch.SelectWeapon
end

function PrevWeapon()
	for i=1,#wswitch.Names do
		j=wswitch.SelectWeapon-i
		if (j<0) then j=j+#wswitch.Names+1 end
		if wswitch.MyWeapons[j] then return j end
	end
	return wswitch.SelectWeapon
end

function CountWeapons()
	ret=0
	for k,v in pairs(wswitch.MyWeapons) do
		if (v==true) then ret=ret+1 end
	end
	return ret
end 

function wswitch.ResetWeapons()
	for k,v in pairs(wswitch.MyWeapons) do
		wswitch.MyWeapons[k]=false
	end
end
